package com.hzqy.service.epg;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.FileUtils;
import com.hzqy.commons.utils.PublicUtil;
import com.hzqy.persistence.dao.epg.AcDrawDao;
import com.hzqy.service.picture.PictureType;
import com.hzqy.web.vo.CmActivityVo;
import com.hzqy.web.vo.epg.AcDrawButtonVo;
import com.hzqy.web.vo.epg.AcDrawChartVo;


@Service
public class AcDrawServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private AcDrawDao acDrawDao;

	//查询所有活动记录列表
	public  Map<String, Object> selectActionList (CmActivityVo adbvo,int pageNo,int pageSize){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		int rowCount = acDrawDao.selectActionCount(adbvo);
		result.put("total", rowCount);
		if(rowCount>0 && pageNo>0 && pageSize>0) {
			map.put("firstResult", (pageNo-1)*pageSize);
			map.put("maxResults", pageSize);
			List<CmActivityVo> list=acDrawDao.selectActionList(map);
			result.put("rows", list);
		}else {
			result.put("rows", new ArrayList<CmActivityVo>());
		}
		return result;
	}

	//添加活动记录
	public  Map<String, Object> insertAction (CmActivityVo adcvo){
		int list = acDrawDao.insertAction(adcvo);
		Map<String, Object> result = new HashMap<String, Object>();
		if( list >0) {
			result.put("success", true);
			result.put("result", "修改成功!");
		}else{
			result.put("success", false);
			result.put("result", "修改失败!");
		}
		return result;
	}
	//修改活动记录
	public  Map<String, Object> updateAction (CmActivityVo adcvo){
		int list = acDrawDao.updateAction(adcvo);
		Map<String, Object> result = new HashMap<String, Object>();
		if( list >0) {
			result.put("success", true);
			result.put("result", "修改成功!");
		}else{
			result.put("success", false);
			result.put("result", "修改失败!");
		}
		return result;
	}
	
	//删除活动记录
	public  Map<String, Object> deleteAction (int f_aca_id){
		int list = acDrawDao.deleteAction(f_aca_id);
		Map<String, Object> result = new HashMap<String, Object>();
		if( list >0) {
			result.put("success", true);
			result.put("result", "修改成功!");
		}else{
			result.put("success", false);
			result.put("result", "修改失败!");
		}
		return result;
	}
	//查询所有活动记录列表
	public  CmActivityVo seletcActionById (int f_aca_id){
		CmActivityVo list = acDrawDao.seletcActionById(f_aca_id);
		return list;
	}
	//活动页面编辑相关业务逻辑处理
	public int actionViewUpdate(JSONObject jsonObject) {
		AcDrawChartVo adcv = new AcDrawChartVo();
		adcv.setF_adc_activityid(jsonObject.getInteger("f_adc_activityid"));
		adcv.setF_adc_type(jsonObject.getString("f_adc_type"));
		int f_adb_draw_id =jsonObject.getInteger("f_adc_id");
		int flag=0;
		if(f_adb_draw_id == 0) {//不存在页面id
			flag=acDrawDao.insertchar(adcv);
			f_adb_draw_id=adcv.getF_adc_id();
		}else {
			flag=acDrawDao.updatetchar(adcv);
		}
		//删除该页面相关所有按钮  重新插入
		flag = acDrawDao.deleteButton(f_adb_draw_id);
		JSONArray jsonArray = (JSONArray) jsonObject.get("view");
		String jsonArrayStr=JSON.toJSONString(jsonArray);
		List<Object> list =JSON.parseArray(jsonArrayStr);
		if(flag!=-1 && list != null ){
			AcDrawButtonVo vo = new AcDrawButtonVo();
			for(int i=0;i<list.size();i++){
				Map<String,Object> map = (Map<String, Object>) list.get(i);
				JSONObject objdata =(JSONObject) JSON.toJSON(map);
				vo.setF_adb_focurl(objdata.getString("f_adb_focurl"));
				vo.setF_adb_heght(objdata.getInteger("f_adb_heght"));
				vo.setF_adb_width(objdata.getInteger("f_adb_width"));
				vo.setF_adb_left(objdata.getInteger("f_adb_left"));
				vo.setF_adb_top(objdata.getInteger("f_adb_top"));
				vo.setF_adb_url(objdata.getString("f_adb_url"));
				vo.setF_adb_info(objdata.getString("f_adb_info"));
				vo.setF_adb_type(objdata.getString("f_adb_type"));
				vo.setF_adb_desc(objdata.getString("f_adb_desc"));
				vo.setF_adb_zIndex(objdata.getString("f_adb_zIndex"));
				vo.setF_adb_pic(objdata.getString("f_adb_pic"));
				vo.setF_adb_father_id(objdata.getIntValue("f_adb_father_id"));
				vo.setF_adb_site(objdata.getString("f_adb_site"));
				vo.setF_adb_draw_id(f_adb_draw_id);
				LOG.debug("vo==="+vo.toString());
				acDrawDao.insertButton(vo);
			}
		}
		return flag;
	}

	//查询某个页面
	public  Map<String, Object> selectChartList (AcDrawChartVo adcvo){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_adc_activityid", adcvo.getF_adc_activityid());
		map.put("f_adc_type", adcvo.getF_adc_type());
		AcDrawChartVo list = acDrawDao.selectChartList(map);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("list", list);
		return result;
	}
	//查询页面上的按钮
	public  Map<String, Object> selectButtonList (AcDrawButtonVo adbvo){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_adc_type", adbvo.getF_adc_type());
		map.put("f_adc_activityid", adbvo.getF_adc_activityid());
		List<AcDrawButtonVo> list = acDrawDao.selectButtonList(map);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("list", list);
		return result;
	}
	public String getFilePath(String picType) {
		String imgFile = "PIC/imgs/upload/"+PictureType.getPicType(picType)+"/"+PublicUtil.getCurDate()+"/";
		return imgFile;
	}
	public String getFileName(String uploadifyFileName) {
		SecureRandom random = new SecureRandom();
		int ranValue = random.nextInt(10000);
		String fileName = PublicUtil.getGenFileName()+ranValue+"."+FileUtils.getExtendName(uploadifyFileName);
		return fileName;
	}
}
